package com.such.kit.database.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.such.kit.easyparam.ParamList;
import com.such.kit.string.StringCommons;
import com.such.kit.validate.ValidateCommons;

/**
 * @author SUCH
 * <pre>
 * 数据迁移配置
 * </pre>
 */
public class MigrationConfig {

	public static final String SCOPE_DEFINITION = "definition";
	public static final String SCOPE_DATA = "data";
	public static final String REMAP_FROM = "from";
	public static final String REMAP_TO = "to";
	public static final String BACKUP_EXPRESSION_KEY = "name";

	/** migrationTableFilters：表过滤器 */
	private List<MigrationTableFilter> migrationTableFilters = new ArrayList<MigrationTableFilter>();
	/** tableSchemaRemap：from 与 to 间的 schema 映射，默认为空，既映射到相同 schema */
	private Map<String, String> tableSchemaRemap = new HashMap<String, String>();

	/** extractScopes：抽取范围，默认为 [SCOPE_DEFINITION | SCOPE_DATA] */
	private Set<String> extractScopes = new HashSet<String>(new ParamList<String>().addParam(SCOPE_DEFINITION).addParam(SCOPE_DATA));
	/** creationDefinitionScript：抽取时生成定义脚本，默认为 false */
	private boolean creationDefinitionScript = false;
	/** doExtractDelete：抽取时生成生成删除语句，默认为 true */
	private boolean doExtractDelete = true;
	/** dataFliters：抽取数据过滤，为指定表名添加 where 条件，通过 where 条件过滤数据 */
	private Map<DDLTableKey, String> dataFliters = new HashMap<DDLTableKey, String>();

	/** compareScopes：比较范围，默认为 [SCOPE_DEFINITION] */
	private Set<String> compareScopes = new HashSet<String>(new ParamList<String>().addParam(SCOPE_DEFINITION));

	/** synchronizeScopes：同步范围，默认为 [SCOPE_DEFINITION | SCOPE_DATA] */
	private Set<String> synchronizeScopes = new HashSet<String>(new ParamList<String>().addParam(SCOPE_DEFINITION).addParam(SCOPE_DATA));
	/** doSynchronizeBackup：同步时是否对原表进行备份，默认为 true */
	private boolean doSynchronizeBackup = true;
	/** backupExpression：备份命名规则表达式，默认加后缀 _mb */
	private String backupExpression = StringCommons.merger("#[", BACKUP_EXPRESSION_KEY, "]_mb");

	/** [参考 {@link #migrationTableFilters}] */
	public List<MigrationTableFilter> getMigrationTableFilters() {
		return this.migrationTableFilters;
	}

	/** [参考 {@link #migrationTableFilters}] */
	public void setMigrationTableFilters(List<MigrationTableFilter> migrationTableFilters) {
		this.migrationTableFilters = migrationTableFilters;
	}

	public void addMigrationTableFilter(MigrationTableFilter migrationTableFilter) {
		this.migrationTableFilters.add(migrationTableFilter);
	}

	public boolean hasTableSchemaRemap() {
		return ValidateCommons.isNotEmpty(this.tableSchemaRemap);
	}

	public void addTableSchemaRemap(String from, String to) {
		// 默认执行双向映射
		this.tableSchemaRemap.put(StringCommons.merger(REMAP_FROM, "_", from), to);
		this.tableSchemaRemap.put(StringCommons.merger(REMAP_TO, "_", to), from);
	}

	public String fromRemapTo(String from) {
		String to = this.tableSchemaRemap.get(StringCommons.merger(REMAP_FROM, "_", from));
		if (ValidateCommons.isEmpty(to)) {
			// 既未进行映射，取相同 schema
			to = from;
		}
		return to;
	}

	public String toRemapFrom(String to) {
		String from = this.tableSchemaRemap.get(StringCommons.merger(REMAP_TO, "_", to));
		if (ValidateCommons.isEmpty(from)) {
			// 既未进行映射，取相同 schema
			from = to;
		}
		return from;
	}

	public Set<String> getExtractScopes() {
		return this.extractScopes;
	}

	public void setExtractScopes(Set<String> extractScopes) {
		this.extractScopes = extractScopes;
	}

	public boolean isCreationDefinitionScript() {
		return this.creationDefinitionScript;
	}

	public void setCreationDefinitionScript(boolean creationDefinitionScript) {
		this.creationDefinitionScript = creationDefinitionScript;
	}

	public boolean isDoExtractDelete() {
		return this.doExtractDelete;
	}

	public void setDoExtractDelete(boolean doExtractDelete) {
		this.doExtractDelete = doExtractDelete;
	}

	/** [参考 {@link #dataFliters}] */
	public Map<DDLTableKey, String> getDataFliters() {
		return this.dataFliters;
	}

	public String getDataFliter(DDLTableKey tableKey) {
		String where = this.dataFliters.get(tableKey);
		if (ValidateCommons.isEmpty(where)) {
			where = "";
		}
		return where;
	}

	/** [参考 {@link #dataFliters}] */
	public void setDataFliters(Map<DDLTableKey, String> dataFliters) {
		this.dataFliters = dataFliters;
	}

	public void addDataFliter(DDLTableKey tableKey, String where) {
		this.dataFliters.put(tableKey, where);
	}

	public Set<String> getCompareScopes() {
		return this.compareScopes;
	}

	public void setCompareScopes(Set<String> compareScopes) {
		this.compareScopes = compareScopes;
	}

	public Set<String> getSynchronizeScopes() {
		return this.synchronizeScopes;
	}

	public void setSynchronizeScopes(Set<String> synchronizeScopes) {
		this.synchronizeScopes = synchronizeScopes;
	}

	public boolean isDoSynchronizeBackup() {
		return this.doSynchronizeBackup;
	}

	public void setDoSynchronizeBackup(boolean doSynchronizeBackup) {
		this.doSynchronizeBackup = doSynchronizeBackup;
	}

	public String getBackupExpression() {
		return this.backupExpression;
	}

	public void setBackupExpression(String backupExpression) {
		this.backupExpression = backupExpression;
	}

}
